﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Touch2Like.Model;

namespace Touch2Like.DAL
{
    public class AuthorizedUserDAL
    {
        #region Add
        public int Add(AuthorizedUserInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@FirstName",SqlDbType.VarChar,50,info.FirstName),
                db.MakeInParam("@LastName",SqlDbType.VarChar,50,info.LastName),
                db.MakeInParam("@Email",SqlDbType.VarChar,50,info.Email),
                db.MakeInParam("@RegistrationCode",SqlDbType.Int,4,info.RegistrationCode),
                db.MakeInParam("@Activated",SqlDbType.Bit,1,info.Activated),
                db.MakeInParam("@CreateUser",SqlDbType.Int,4,info.CreateUser)
            };
            try
            {
                db.ExecNonQuery("CAM_AuthorizedUser_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(AuthorizedUserInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@FirstName",SqlDbType.VarChar,50,info.FirstName),
                db.MakeInParam("@LastName",SqlDbType.VarChar,50,info.LastName),
                db.MakeInParam("@Email",SqlDbType.VarChar,50,info.Email),
                db.MakeInParam("@RegistrationCode",SqlDbType.Int,4,info.RegistrationCode),
                db.MakeInParam("@Activated",SqlDbType.Bit,1,info.Activated),
                db.MakeInParam("@CreateUser",SqlDbType.Int,4,info.CreateUser)
            };
            try
            {
                db.ExecNonQuery("CAM_AuthorizedUser_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Delete
        public void Delete(int listID)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listID)
            };
            try
            {
                db.ExecNonQuery("CAM_AuthorizedUser_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get By Registration Code
        public AuthorizedUserInfo GetByRegistrationCode(int code)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@RegistrationCode",SqlDbType.Int,4,code)
            };
            try
            {
                dr = db.RunProc("CAM_AuthorizedUser_GetByRegistrationCode", param);
                if (dr.Read())
                {
                    AuthorizedUserInfo info = new AuthorizedUserInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.FirstName = DataHelper.GetString(dr["FirstName"]);
                    info.LastName = DataHelper.GetString(dr["LastName"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.RegistrationCode = DataHelper.GetInt(dr["RegistrationCode"]);
                    info.Activated = DataHelper.GetBool(dr["Activated"]);
                    info.CreateUser = DataHelper.GetInt(dr["CreateUser"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By ID
        public AuthorizedUserInfo GetByID(int listID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.VarChar,4,listID)
            };
            try
            {
                dr = db.RunProc("CAM_AuthorizedUser_GetByID", param);
                if (dr.Read())
                {
                    AuthorizedUserInfo info = new AuthorizedUserInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.FirstName = DataHelper.GetString(dr["FirstName"]);
                    info.LastName = DataHelper.GetString(dr["LastName"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.RegistrationCode = DataHelper.GetInt(dr["RegistrationCode"]);
                    info.Activated = DataHelper.GetBool(dr["Activated"]);
                    info.CreateUser = DataHelper.GetInt(dr["CreateUser"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get All
        public DataTable GetAll()
        {
            Database db = new Database();

            try
            {
                return db.GetDataSet("CAM_AuthorizedUser_GetAll").Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion


        public static int AuthorizedUserVerify(string Email, string AuthorizationCode)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                
                db.MakeInParam("@Email",SqlDbType.VarChar,500,Email),
                db.MakeInParam("ActivationCode",SqlDbType.Int,4,AuthorizationCode)
            };

            try
            {
                dr = db.RunProc("CAM_AuthorizedUser_Verify", param);
                if (dr.Read())
                {
                    return DataHelper.GetInt(dr[0]).Value;

                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
    }
}
